
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.query;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import logger.PoolLogger;

import property.Order;
import utility.MergedBuffer;
import utility.Splitstring;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.SearchRecord;

public class Rightouterjoin 
{

	GetIdName gid;
	//String [] buffer2;
	String [] buffer1;
	String[] Querybuffer;
	String JOIN ="join";
	String Message;
	String object;
	String LHS="lhs";
	String RHS="rhs";
	String reporton;
	String conditionopeartor="conditionopeartor";
	SearchRecord sr;
	StringBuffer heading = new StringBuffer(); 
	int index1[];
	int index2[];
	Hashtable<Object, Object> joinmap = new Hashtable<Object, Object>();
	String tablename;
	int indexofbuffer1=-1;
	int indexofbuffer2=-1;
	String key="no$";
	String value="no$";
	String Reportname;
	Hashtable<Object, Object > Querymap = new Hashtable<Object, Object>();
	String tid;
	PoolLogger pl;
	
	public Rightouterjoin() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public void setObject(String object)
	{
		this.object = object; 
	}
	
	public void getResult()
	{	
		String lhs = joinmap.get(LHS).toString().toLowerCase();
		String rhs = joinmap.get(RHS).toString().toLowerCase();
		//String heading1 =  Addprefix.removerefix(buffer1[0]);
		String heading1 =  (buffer1[0]);
		//String heading2 = Addprefix.removerefix(Querybuffer[0]);
		//String lOgicaloperator = joinmap.get(conditionopeartor).toString();
		indexofbuffer1 = getFieldinbuffer(heading1,rhs);
		indexofbuffer2 = getFieldinbuffer(Querybuffer[0],lhs);
		if(indexofbuffer2==-1)
			indexofbuffer2= getFieldinbuffer( (Querybuffer[0]),lhs);
		/*Querybuffer[0] = Addprefix.removerefix(Querybuffer[0]);
		Querybuffer[0]=Addprefix.addPrefix(Querybuffer[0],reporton);*/
		MergedBuffer merg = new MergedBuffer();
		this.buffer1 =merg.rightMergedBuffer(buffer1,Querybuffer, indexofbuffer1, indexofbuffer2).toString().split("\n"); 
		/*getIndexArray();
		String split[]=buffer1[0].split("\t");
		for(int i=0;i<index1.length;i++)
		{	
			heading.append(split[index1[i]]+"\t"); 
		}
		split=Querybuffer[0].split("\t");
		for(int i=0;i<index2.length;i++)
		{	
			heading.append(split[index2[i]]+"\t"); 
		}
		heading.append("\n");
		for(int i=1;i<buffer1.length;i++)
		{
			String splitbuffer1[] = buffer1[i].split("\t");
			for(int j=1;j<Querybuffer.length;j++)
			{
				try 
				{
					String splitbuffer2[] = Querybuffer[j].split("\t");
					if(splitbuffer1[indexofbuffer1].toLowerCase().equals(splitbuffer2[indexofbuffer2].toLowerCase()))
					{
						try 
						{
							for(int k=0;k<index1.length;k++)
								Finalresult.append(splitbuffer1[index1[k]]+"\t");
						}
						catch (Exception e) 
						{
							//e.printStackTrace();
						}
						try 
						{
							for(int k=0;k<index2.length;k++)
								Finalresult.append(splitbuffer2[index2[k]]+"\t");
						}
						catch (Exception e) 
						{	
							//e.printStackTrace();
						}
						Finalresult.append("\n");
					}
				}
				catch (Exception e) 
				{
					//e.printStackTrace();
				}
				
			}
		}
		heading.append(Finalresult);
		*/ 
		this.buffer1 = removeDuplicateRecord(buffer1);
		//this.buffer1 = (heading.toString().split("\n"));
	}

	public String getObject() 
	{
		return object;
	}

	public String[] getbuffer() 
	{
		return buffer1;
	}

	public void initializeData()
	{

		RecordSearch rs =new RecordSearch();
		Hashtable<Object, Object> table =new Hashtable<Object, Object>();
		table.put("mid", object);
		table.put("td", "null");
		rs.setConditionMap(table);
		String result[] = rs.getArrayFromResultSet();
		for(int i=0;i<result.length;i++)
		{
			String splitresult[] = result[i].split("\t");
			String value = splitresult[Column.pv_index-1];
			if(value.equals("0"))
				value  = splitresult[Column.vt_index-1];
			else
				value = gid.getItem(value);
			joinmap.put(splitresult[Column.pid_index-1],value );
		}
	}

	public void setDbmanager()
	{
		gid = new GetIdName();
		LHS = gid.getId(LHS);
		RHS = gid.getId(RHS);
		JOIN = gid.getId(JOIN);
		conditionopeartor = gid.getId(conditionopeartor);
	}

	public void setbuffer(String[] buffer) 
	{
		this.buffer1 = buffer;
	}
	public int getFieldinbuffer(String buffer,String Field)
	{
		String split[] = buffer.split("\t");
		for(int i=0;i<split.length;i++)
		{	
			
			if(split[i].toLowerCase().equals(Field.toLowerCase()))
				return i;
		}
		return -1;
	}
	 public void getIndexArray()
	 {
		 String split1[] = buffer1[0].split("\t");
		 String split2[] = Querybuffer[0].split("\t");
		 index1 = new int[split1.length];
		 index2 = new int[split2.length-1];
		 int count=0;
		 for(int i=0;i<split1.length;i++)
		 {
			 index1[count] =i;
			 count++;
		 }
		 count=0;
		 for(int i=0;i<split2.length;i++)
		 {
			 if(i!=indexofbuffer2)
			 {
				index2[count] = i;
			 	count++;
			 }	
		 }
	 }

	public String gettablename() 
	{
		return tablename;
	}

	public void setQuerybuffer(String[] Querybuffer) 
	{
		this.Querybuffer =Querybuffer;
	}

	public void settablename(String tablename) 
	{
		this.tablename = tablename;
	}
	public String[] getQuerybuffer() {
		return Querybuffer;
	}


	public void setKey(String key) 
	{
		this.key = key;
	}


	public void setValue(String value) 
	{
		this.value = value;
	}


	public String getkey() 
	{
		return key;
	}


	public String getvalue() 
	{
		return value;
	}


	public String getQueryName() 
	{
		return null;
	}

	public String [] removeDuplicateRecord(String[] buffer)
	{
		Hashtable<Object, Object> table =new Hashtable<Object, Object>();
		for(int i=1;i<buffer.length;i++)
		{
			table.put(buffer[i], "0");
		}
		String newbuffer[] = new String[table.size()+1];
		newbuffer[0] =buffer[0];
		int count=1;
		Enumeration<Object> enumfortable = table.keys();
		while(enumfortable.hasMoreElements())
		{
			newbuffer[count] = enumfortable.nextElement().toString();
			count++;
		}
		return newbuffer;
	}
	public void setQueryName(String QueryName) 
	{
	
		
	}
	public Hashtable<Object, Object> getQueryMap() 
	{
		return Querymap;
	}


	public void setQueryMap(Hashtable<Object, Object> QueryMap) 
	{
		this.Querymap = QueryMap;
	}


	public String gettid() 
	{
		return tid;
	}

	public void settid(String tid) 
	{
		this.tid =tid;	
	}


	public void addReport() 
	{
		getResult();
	}


	public void updateReport() 
	{
		getResult();
	}


	public String getReportname() 
	{
		return Reportname;
	}


	public void setReportName(String Reportname) 
	{
		this.Reportname = Reportname;
	}


	public void setreporton(String reporton) 
	{
	this.reporton =reporton;
	}


	public void setReportmap(Hashtable<Object, Object> ReportMap) 
	{
		
	}

	public void settypeofreport(String typeofreport) 
	{
		
	}
	public void setViewObject(String Object,char ch)
	{
		Splitstring sp =new Splitstring();
	    String splitobject[] = sp.split(Object,Character.toString(ch));
	    Order order = new Order();
	    Hashtable<Object, Object>table =order.getSequences(JOIN);
	    for(int j=0;j<table.size();j++)
	    {
		    for(int i=0;i<splitobject.length;i++)
	    	{
	    		joinmap.put(gid.getId(table.get(Integer.toString(j+1)).toString()), splitobject[i]);
	    		j++;
	    	}
	    }
	 }
		
	@SuppressWarnings("unchecked")
	public void setViewObject(Vector<Object> PropertyMap) 
	{
		try 
		{
			joinmap.putAll((Map<? extends Object, ? extends Object>) PropertyMap.get(0));
		} catch (Exception e) 
		{
			
		}
	}	


}
